<?php

namespace app\api\controller;

use app\api\lib\SuPengJun;
use app\common\model\dynamic\DynamicCommentModel;
use app\common\model\NoticeModel;
use app\common\model\user\UserModel;
use app\api\service\DynamicService;
use think\Request;

/**
 * 评论
 */
class Comment extends Controller
{
    protected array $middleware = [
        'api' => ['except' => ['list', 'son']],
        'orCheck' => ['only' => ['list', 'son']]
    ];

    /**
     * 发送评论
     */
    public function save(Request $request)
    {
        $uid = $request->uid;
        if (UserModel::where('id', $uid)->value('status') == 0) return error('当前账号已被禁言！');
        $id = $request->post('id');
        $dUid = $request->post('d_uid');
        $dImg = $request->post('d_img');
        $dContent = $request->post('d_content');
        $content = $request->post('content');
        $replyUserId = $request->post('reply_user_id', 0);
        $replyCommentId = $request->post('reply_comment_id', 0);
        $contentUrl = '/pages/note/details?id=' . $id;
        $model = [
            'dynamic_id' => $id,
            'user_id' => $uid,
            'reply_user_id' => $replyUserId,
            'reply_comment_id' => $replyCommentId,
            'content' => $content,
            'status' => 0,
            'create_time' => time()
        ];
        $model = SuPengJun::ipStr($model, $request->ip());
        $cid = (new DynamicCommentModel)->insertGetId($model);
        if ($uid != $replyUserId && $uid != $dUid) {
            $avatarUrl = '/pages/user/details?id=' . $uid;
            $title = '回复了你的评论';
            if (!$replyUserId || $replyUserId == 0) {
                $title = '评论了你的笔记';
                $replyUserId = $dUid;
            }
            if ($dImg) {
                $contentStr = $content . '<br>' . '<div style="margin-top: 4px;border-left: 4px solid #f5f5f5;padding-left: 8px;">
                <img src="' . $dImg . '" style="width: 48px;height: 48px;border-radius: 4px;object-fit: cover;display: block;" /></div>';
            } else {
                $contentStr = $content . '<br><p style="margin-top: 8px;color:#999;border-left: 4px solid #f5f5f5;padding-left: 8px;font-size: 11px;">' . SuPengJun::shorteStr($dContent) . '</p>';
            }
            (new NoticeModel())->send($replyUserId, $title, $contentStr, $uid, $avatarUrl, $contentUrl, 1);
        }
        $item = DynamicCommentModel::where('id', $cid)->field('id,user_id,reply_user_id,content,province,status')->find();
        if ($item['reply_user_id']) $item['reply_user'] = UserModel::where('id', $item['reply_user_id'])->field('id,name')->find();
        $item['user'] = UserModel::where('id', $item['user_id'])->field('id,name,avatar')->find();
        $item['province'] = SuPengJun::addsStr($item['province']);
        $item['create_time_str'] = "刚刚";
        $item['list_count'] = 0;
        event('CommentAudit', ['cid' => $cid]);
        return success('评论成功 🎉', $item);
    }

    /**
     * 评论列表
     */
    public function list(Request $request)
    {
        $id = $request->get('id');
        $type = $request->get('type', 0);
        $order = ['id' => 'asc'];
        if ($type == 1) $order = ['id' => 'desc'];
        $data = DynamicCommentModel::where('dynamic_id', $id)
            ->where('reply_comment_id', 0)
            ->order($order)
            ->field('id,user_id,content,province,city,country,create_time,status')
            ->paginate(8)->toArray();
        foreach ($data['data'] as $k => $v) {
            $list = DynamicCommentModel::where('reply_comment_id', $v['id'])
                ->order('id', 'asc')
                ->field('id,user_id,content,province,city,country,reply_user_id,create_time,status')
                ->limit(4)
                ->select()->toArray();
            foreach ($list as $ik => $iv) {
                $list[$ik]['content'] = DynamicService::comment_status($iv['content'], $iv['status']);
                $list[$ik]['user'] = UserModel::where('id', $iv['user_id'])->field('id,name,avatar')->find();
                $list[$ik]['reply_user'] = UserModel::where('id', $iv['reply_user_id'])->field('id,name')->find();
                $iProvince = $iv['province'];
                if (!$iProvince) $iProvince = $iv['city'];
                if (!$iProvince) $iProvince = $iv['country'];
                $list[$ik]['province'] = SuPengJun::addsStr($iProvince);
                $list[$ik]['create_time_str'] = SuPengJun::timeStr($iv['create_time']);
            }
            $data['data'][$k]['list_count'] = 0;
            if (count($list)) {
                $data['data'][$k]['list_count'] = DynamicCommentModel::where('reply_comment_id', $v['id'])->count();
                $data['data'][$k]['list'] = $list;
            }
            $data['data'][$k]['content'] = DynamicService::comment_status($v['content'], $v['status']);
            $data['data'][$k]['user'] = UserModel::where('id', $v['user_id'])->field('id,name,avatar')->find();
            $province = $v['province'];
            if (!$province) $province = $v['city'];
            if (!$province) $province = $v['country'];
            $data['data'][$k]['province'] = SuPengJun::addsStr($province);
            $data['data'][$k]['create_time_str'] = SuPengJun::timeStr($v['create_time']);
        }
        return success('success', $data);
    }

    /**
     * 获取子评论
     */
    public function son(Request $request)
    {
        $id = $request->get('id');
        $data = DynamicCommentModel::where('reply_comment_id', $id)
            ->order('id', 'asc')
            ->field('id,user_id,content,province,city,country,reply_user_id,create_time,status')
            ->paginate(4)->toArray();
        foreach ($data['data'] as $k => $v) {
            $data['data'][$k]['content'] = DynamicService::comment_status($v['content'], $v['status']);
            $data['data'][$k]['user'] = UserModel::where('id', $v['user_id'])->field('id,name,avatar')->find();
            $data['data'][$k]['reply_user'] = UserModel::where('id', $v['reply_user_id'])->field('id,name')->find();
            $data['data'][$k]['province'] = SuPengJun::addsStr($v['province']);
            $data['data'][$k]['create_time_str'] = SuPengJun::timeStr($v['create_time']);
        }
        return success('success', $data);
    }

    /**
     * 删除评论
     * @param array $id 评论ID
     * @return json|array
     * @throws \AlibabaCloud|Client|Result|array
     */
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        DynamicCommentModel::where('id', $id)->update(['status' => 3]);
        return success('已删除该评论！');
    }
}
